<?php

namespace addons\prl\controller;
use addons\prl\model\Channels as ChannelsModel;
use addons\prl\model\Views as ViewsModel;
use think\Db;
use think\Session;
class Forms extends Base
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        $id = $this->request->param('id');
        $this->current_channel = (new ChannelsModel)->channel($id)->channelorder()->find();
        if(empty($this->current_channel))
            return $this->error('栏目未找到！');

        $this->view_type = 5;
        parent::_initialize();
    }

    //表单页
    public function index()
    {
        if($this->request->isPost())
        {
            $putIds = Session::get('putForms', 'forms');//获取表单提交记录
            if(in_array($putIds, $this->current_channel->forms->id)){
                $this->error('请勿重复提交!');
            }
            $data = $this->request->param('row/a');
            $row = Db::table($this->current_channel->forms->table)->insert($data);
            if($row)
            {
                $putIds[] = $this->current_channel->forms->id;
                session('putForms', $putIds, 'forms');
                $this->success('提交成功');
            }
            $this->error('提交失败!');
        }
        $viewName = ViewsModel::where('id', $this->current_channel->form_view_id)->value('tpl');
        return $this->show($viewName);
    }
    
}
